package com.ice.base.controller;

import com.ice.base.constant.BaseConstant;
import com.ice.base.model.SysMenu;
import com.ice.base.service.SysMenuService;
import com.ice.base.util.BaseUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@ApiIgnore(value = "系统后台-菜单管理")
public class SysMenuController  extends BaseController  {

    //---------------------------菜单-----------------------start
    @Resource
    private SysMenuService sysMenuService;

    @ApiOperation(value = "菜单-插入")
    @PostMapping("/base/SysMenu/insert")
    public HashMap insert_SysMenu(HttpServletRequest request, SysMenu sysMenuModel) {
        HashMap<String, Object> returnMap = getReturnMap(BaseConstant.Response_MENU.REQUEST_INSERT_FAILED);
        int menuLeave = sysMenuModel.getMenu_level();
        String pid = sysMenuModel.getMenu_parent_id();
        String menuName = sysMenuModel.getMenu_name();

        if(menuLeave != 0 && (StringUtils.isEmpty(pid)||"无".equals(pid))){
            returnMap.put("message","非一级菜单父id不能为空！");
            return returnMap;
        }
        if(StringUtils.isEmpty(menuName)){
            returnMap.put("message","菜单名称不能为空！");
            return returnMap;
        }
        try{
            sysMenuModel.getInstanceForInsert(request, sysMenuModel);
            //定制
            sysMenuModel.setId(BaseUtil.getUUID());
            int insert = sysMenuService.insertRBAC(sysMenuModel);
            if (insert != 0) {
                returnMap.put("data", sysMenuModel);
                getReturnMapSuccess(returnMap);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return returnMap;
    }


    @ApiOperation(value = "菜单-删除")
    @PostMapping("/base/SysMenu/deleteById")
    public HashMap deleteById_SysMenu(@RequestParam("id") String id) {
        HashMap<String, Object> returnMap = getReturnMap();
        Integer delete = sysMenuService.deleteRBAC(id);
        if (delete != null && delete != 0) {
            getReturnMapSuccess(returnMap);
        }
        return returnMap;
    }

    @ApiOperation(value = "菜单-更新")
    @PostMapping("/base/SysMenu/updateById")
    public HashMap update_SysMenu(HttpServletRequest request, SysMenu sysMenuModel) {
        HashMap<String, Object> returnMap = getReturnMap();
      /*  String createTime = request.getParameter("createTime");
        if (createTime != null) {
            Date date = new Date(Long.parseLong(createTime));
            sysMenuModel.setCreateTime(date);
        }
*/
        int update = sysMenuService.updateRBAC(sysMenuModel);
        if (update != 0) {
            getReturnMapSuccess(returnMap);
        }
        return returnMap;
    }


    @ApiIgnore(value = "菜单-查询一个")
    @PostMapping("/base/SysMenu/selectById")
    public HashMap selectById_SysMenu(@RequestParam("id") String id) {
        return selectById(sysMenuService, id);
    }

    @ApiIgnore(value = "菜单-查询多个")
    @PostMapping("/base/SysMenu/selectAll")
    public HashMap selectList_SysMenu(HttpServletRequest request) {
        return selectListByCondition(request, sysMenuService, new SysMenu());
    }


    @ApiOperation(value = "菜单-查询多个(当前用户)")
    @PostMapping("/base/SysMenu/tree/selectRBAC")
    public List selectTreeRBAC_SysMenu() {
        List<SysMenu> sysMenuModels = new ArrayList<>();
        try{
            sysMenuModels = sysMenuService.selectRBAC();
        }catch (Exception e){
            e.printStackTrace();
        }
        return sysMenuModels;
    }

    @ApiOperation(value = "菜单-查询多个(全部)")
    @PostMapping("/base/SysMenu/tree/selectAll")
    public ArrayList selectTree_SysMenu() {
        return sysMenuService.selectAll();
    }

    @ApiIgnore(value = "菜单-查询分页")
    @PostMapping("/base/SysMenu/selectListByPageHelper")
    public HashMap selectListByPageHelper_SysMenu(HttpServletRequest request,
                                                  @RequestParam(name = "pageSize") Integer pageSize,
                                                  @RequestParam(name = "pageNum") Integer pageNum) {
        return selectListByPageHelper(request, sysMenuService, new SysMenu());
    }



    //---------------------------菜单-----------------------end


}
